<template>
	<view class="deta">
		<uv-navbar leftIcon="arrow-leftward" leftIconColor="#FFF" placeholder autoBack bgColor="#161714">
			<template v-slot:center>
				<view class="center_slot">{{product.name}}</view>
			</template>
			<!-- <template v-slot:right>
				<view class="right_slot">
					<image src="@/static/images/6.png"></image>
				</view>
			</template> -->
		</uv-navbar>
		<scroll-view scroll-y class="scroll_view">
			<view class="content">
				<image :src="product.cover" mode="widthFix" class="cover"></image>
				<view class="info_view">
					<view class="name">【{{product.name}}】</view>
					<view class="list">
						<view class="item">
							<view class="key">发行总量</view>
							<view class="value">{{product.issue_all}}份</view>
						</view>
						<view class="item">
							<view class="key">发售时间</view>
							<view class="value">{{data_info.start_date}}</view>
						</view>
						<view class="item">
							<view class="key">每人限购</view>
							<view class="value">{{data_info.quota}}份</view>
						</view>
						<view class="item">
							<view class="key">剩余数量</view>
							<view class="value">{{data_info.cache_stock}}份</view>
						</view>
					</view>
				</view>
				<view class="title">NFT简介</view>
				<view class="parse_view">
					<uv-parse :content="product.content"></uv-parse>
				</view>
			</view>
		</scroll-view>
		<view class="footer" v-if="data_info.status>0">
			<view class="left">
				<view class="unit">$</view>
				<view class="value">{{product.price}}</view>
			</view>
			<view class="btn" @click="buy" v-if="data_info.status==1">购买</view>
			<view class="btn end" v-else-if="data_info.status==2">已结束</view>
			<view class="btn end" v-else>已售罄</view>
		</view>
		<view class="footer count_down" v-else>
			<uv-count-down :time="time" format="DD:HH:mm:ss" autoStart millisecond @change="onChange"
				@finish="onFinish">
				<view class="time">
					<text class="time__item">{{ timeData.days }}&nbsp;天</text>
					<text class="time__item">{{ timeData.hours>10?timeData.hours:'0'+timeData.hours}}&nbsp;时</text>
					<text class="time__item">{{ timeData.minutes }}&nbsp;分</text>
					<text class="time__item">{{ timeData.seconds }}&nbsp;秒</text>
				</view>
			</uv-count-down>
		</view>
		<uv-popup ref="popup" round="10" bgColor="none" mode="bottom">
			<CodeInput @close="popup.close" @submit="submit" />
		</uv-popup>
	</view>
</template>

<script setup>
	import {
		purchaseInfo,
		purchaseBuy
	} from "@/request/api";
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import {
		ref,
		shallowRef
	} from "vue";
	import CodeInput from "@/components/home/CodeInput.vue"
	import useBase from "@/hooks/useBase.js";
	const {
		router
	} = useBase();

	const popup = shallowRef();
	const buy = () => popup.value.open();

	const submit = async (value) => {
		let {
			order_id
		} = await purchaseBuy({
			id: data_info.value.id,
			pay_password: value
		});
		popup.value.close();
		router(`/pages/common/order_result?id=${order_id}`);
	};

	const data_info = ref({});
	const product = ref({});

	let id = "";
	const time = ref(0);
	const getDeta = async () => {
		let {
			info,
			product: product_value
		} = await purchaseInfo({
			id
		});
		data_info.value = info;
		product.value = product_value;

		let nowTime = Number(new Date());
		let startTime = info?.start * 1000;
		if (startTime <= nowTime) return;
		time.value = Number(startTime) - Number(nowTime);
	};
	onLoad((options) => {
		id = options.id;
		getDeta();
	});

	const timeData = ref({});
	const onChange = (e) => timeData.value = e;
	const onFinish = () => {
		if (time.value <= 0) return;
		time.value = 0;
		getDeta();
	};
</script>

<style scoped lang="scss">
	$footerHeight: 108rpx;

	.deta {
		height: 100vh;
		overflow: hidden;

		.center_slot {
			color: #FFFFFF;
			font-size: 14px;
			font-weight: 800;
		}

		.right_slot {
			image {
				width: 24px;
				height: 24px;
			}
		}

		.scroll_view {
			height: calc(100vh - #{$navBarHeight} - #{$footerHeight});

			.content {
				padding: 20rpx;
				box-sizing: border-box;

				.cover {
					width: 100%;
					border-radius: 20rpx;
				}

				.info_view {
					border-radius: 20rpx;
					margin-top: 20rpx;
					margin-bottom: 10rpx;
					padding: 0 30rpx;
					box-sizing: border-box;
					background: #2E2E2E;

					.name {
						color: #FFFFFF;
						font-size: 32rpx;
						font-weight: 800;
						border-bottom: 1px solid #42433F;
						padding: 40rpx 0;
						box-sizing: border-box;
					}

					.list {
						padding-top: 20rpx;
						box-sizing: border-box;
						display: flex;
						flex-wrap: wrap;
						justify-content: space-between;

						.item {
							width: 50%;
							padding-bottom: 35rpx;
							box-sizing: border-box;

							.key {
								color: #808080;
								font-size: 26rpx;
								padding-bottom: 20rpx;
								box-sizing: border-box;
							}

							.value {
								color: #FFFFFF;
								font-size: 40rpx;
								font-family: DINPro;
							}
						}
					}
				}

				.title {
					height: 90rpx;
					line-height: 90rpx;
					color: #FFFFFF;
					font-size: 32rpx;
					font-weight: 800;
					padding-left: 30rpx;
					box-sizing: border-box;
					position: relative;

					&::after {
						content: "";
						width: 8rpx;
						height: 30rpx;
						background: #DDF247;
						position: absolute;
						left: 11rpx;
						top: 50%;
						transform: translateY(-50%);
					}
				}

				.parse_view {
					color: #999999;
					font-size: 26rpx;
				}
			}
		}

		.footer {
			height: $footerHeight;
			padding: 0 30rpx;
			box-sizing: border-box;
			background: #111111;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.left {
				display: flex;
				align-items: flex-end;

				.unit {
					color: #DDF247;
					font-size: 40rpx;
				}

				.value {
					color: #DDF247;
					font-size: 60rpx;
					font-family: DINPro;
				}
			}

			.btn {
				width: 320rpx;
				height: 88rpx;
				line-height: 88rpx;
				text-align: center;
				color: #111111;
				font-size: 28rpx;
				font-weight: bold;
				border-radius: 20rpx;
				background: #DDF247;

				&.end {
					color: #111111;
					background: #CCCCCC;
				}
			}

			&.count_down {
				justify-content: center;

				.time {
					display: flex;
					align-items: center;

					&__item {
						color: #DDF247;
						font-size: 60rpx;
						font-family: DINPro;
					}
				}
			}
		}
	}
</style>